package com.lst.servlet;


import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.MessageReqPara;
import com.lst.model.Message;
import com.lst.service.MessageService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: MessageAddServlet
 * @Description: 消息添加接口
 * 
 */
public class MessageAddServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private MessageService messageService = ctx.getBean(MessageService.class);
	

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		BaseResponse resPara = new BaseResponse();

		try {
			MessageReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			Integer sendId = Integer.valueOf(reqPara.getUserid());
			Integer receptionId = Integer.valueOf(reqPara.getReceptionId());	
			String content = reqPara.getContent();
	
			Message message=new Message();
			message.setSendId(sendId);
			message.setContent(content);
			message.setReceptionId(receptionId);
			message.setIsDelete(false);
			message.setReadStatus(false);
			message.setCreateDate(startDate);
			
	        messageService.insert(message);
	        
			resPara.setCode(CommCode.M_Y000000);
			resPara.setMessage(CommCode.M_Y000001);
	
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("MessageAddServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

	//		logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public MessageReqPara getReqPara(HttpServletRequest request) {

		MessageReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new MessageReqPara());

			reqPara = (MessageReqPara) baseRequest.clone();
			
			String userid = request.getParameter("userid");
			String content = request.getParameter("content");
			String receptionId = request.getParameter("receptionId");

//			logger.info("reqPara userid: " + userid);
//			logger.info("reqPara content: " + content);
//			logger.info("reqPara receptionId: " + receptionId);

	        reqPara.setUserid(userid);
			reqPara.setContent(content);
	        reqPara.setReceptionId(receptionId);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		MessageReqPara cdReqPara = (MessageReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		if (StringUtils.isEmpty(cdReqPara.getContent())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("消息为空");
		}else if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		}else if (StringUtils.isEmpty(cdReqPara.getReceptionId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("接收人id为空");
		} else {
			flag = true;
		}

		return  flag;
	}
	
	

}
